/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kylingroup.Desktop.Utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javafx.concurrent.Task;

/**
 *
 * @author kylin
 */
public class Extractor extends Task<HashMap<String,String>>{
    private String extractdir=null;//System.getProperty("user.dir")+"/plugins";
    private String folder="plugins";
    private String filename=null;
    private File sourcefile=null;
    private int buffersize=0;
    private int sleep=0;
    private HashMap<String,String> info=null;
            
    public Extractor(File sourcefile,String extractdir,int buffersize,int sleep){
        this.sourcefile=sourcefile;
        filename=this.sourcefile.getName();
        this.extractdir=extractdir;
        this.buffersize=buffersize;
        this.sleep=sleep;
        info=new HashMap<String,String>();
        filename=filename.substring(0,filename.lastIndexOf("."));
        this.extractdir=extractdir+"/"+folder;
        //System.out.println("filename:"+filename);
    }
    
    @Override
    protected HashMap<String, String> call() throws Exception {
        try{
            File extractfolder=new File(extractdir);
            if(!extractfolder.exists()){
                extractfolder.mkdirs();
            }
            
            BufferedOutputStream dest=null;
            BufferedInputStream is=null;
            ZipEntry entry;
            ZipFile zipfile=new ZipFile(sourcefile);

            Enumeration e1=zipfile.entries();
            Enumeration e2=zipfile.entries();
            
            long size=0;
            int rlength=0;
            
            //get uncompressed total size
            while(e1.hasMoreElements()){
                entry=(ZipEntry)e1.nextElement();
                size+=entry.getSize();
            }
            
            String subfile="";
            while(e2.hasMoreElements()){
                entry=(ZipEntry)e2.nextElement();
                subfile=extractdir+File.separator+entry.getName();
                
                if(entry.isDirectory()){
                    makeDir(subfile);
                }else{
                    is=new BufferedInputStream(zipfile.getInputStream(entry));
                    int ins;
                    
                    byte data[] = new byte[buffersize];
                    FileOutputStream fos = new FileOutputStream(subfile);
                    
                    dest = new BufferedOutputStream(fos,buffersize);
                    while((ins=is.read(data,0,data.length))!=-1){
                        dest.write(data,0,ins);
                        rlength+=ins;
                        updateProgress(rlength,size);
                        Thread.sleep(sleep);
                    }
                    dest.flush();
                    dest.close();
                    is.close();
                }
            }
            info.put("sourcefile", sourcefile.getAbsolutePath());
            info.put("extractdir", folder+"/"+filename);
            info.put("size", String.valueOf(size));
            info.put("buffersize", String.valueOf(buffersize));

        }catch(Exception e){
            e.printStackTrace();
        }
        return info;
    }
    
    private boolean makeDir(String unZipDir){
        boolean b=false;
        try{
            File f=new File(unZipDir);
            if(!f.exists()){
                b = f.mkdirs();
            }
        }catch(Exception e) {
            e.printStackTrace();
            return b;
        }
        return b;
    }
    
}